//
//  ClubNormalSettingCell.swift
//  77Club
//
//  Created by Apple on 2024/9/18.
//

import UIKit

class ClubNormalSettingCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .white
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubNormalSettingItemCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 64.0.Scale
        tableView.layer.cornerRadius = 8.0.Scale
        tableView.layer.masksToBounds = true
        return tableView
    }()

    var models: [ClubSettingModel]?{
        didSet{
            tableView.reloadData()
        }
    }
    var yuyueGameBlock: ((_ gameId: String) -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            tableView
        }
        
        tableView.leading(12.0.Scale).trailing(12.0.Scale).top(0).bottom(16.0.Scale)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return models?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubNormalSettingItemCell", for: indexPath) as! ClubNormalSettingItemCell
        cell.model = models?[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ClubNormalSettingItemCell: UITableViewCell {
    
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(15.font_medium).end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#AEB0B3").font(12.font_medium).end()
    private let switchButton = c_make_ex(UIButton.self)
        .image("Mine_settings_switchOff").image("Mine_settings_switchOn",state: .selected).end()

    var model:ClubSettingModel?{
        didSet{
            guard let md = model else { return }
            
            titleLabel.text = md.title
            subLabel.text = md.content
            if md == .update {
                switchButton.isSelected = UserDefaultsManager.getBool(forKey: .versionUpdate)
            }else if md == .mode {
                switchButton.isSelected = UserDefaultsManager.getBool(forKey: .saveMode)
            }else if md == .active {
                switchButton.isSelected = UserDefaultsManager.getBool(forKey: .activeAlert)
            }else if md == .chat {
                switchButton.isSelected = UserDefaultsManager.getBool(forKey: .chatAlert)
            }else if md == .integral {
                switchButton.isSelected = UserDefaultsManager.getBool(forKey: .integralAlert)
            }else if md == .open {
                switchButton.isSelected = UserDefaultsManager.getBool(forKey: .gameAlert)
            }else if md == .gift {
                switchButton.isSelected = UserDefaultsManager.getBool(forKey: .giftAlert)
            }else if md == .signin {
                switchButton.isSelected = UserDefaultsManager.getBool(forKey: .signAlert)
            }else if md == .personal {
                switchButton.isSelected = UserDefaultsManager.getBool(forKey: .personalAlert)
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            titleLabel
            subLabel
            switchButton
        }
        
        titleLabel.leading(12.0.Scale).centerVertically()
        switchButton.trailing(12.0.Scale).width(52.0.Scale).height(32.0.Scale).centerVertically()
        subLabel.centerVertically()
        subLabel.Leading == titleLabel.Trailing
        
        switchButton.addTarget(self, action: #selector(clickSwitchButton), for: .touchUpInside)
    }
    
    @objc private func clickSwitchButton(){
        
        switchButton.isSelected = !switchButton.isSelected
        guard let md = model else { return }
        let _: Bool = switchButton.isSelected
        if md == .update {
            UserDefaultsManager.setBool(switchButton.isSelected, forKey: .versionUpdate)
        }else if md == .mode {
            UserDefaultsManager.setBool(switchButton.isSelected, forKey: .saveMode)
        }else if md == .active {
            UserDefaultsManager.setBool(switchButton.isSelected, forKey: .activeAlert)
        }else if md == .chat {
            UserDefaultsManager.setBool(switchButton.isSelected, forKey: .chatAlert)
        }else if md == .integral {
            UserDefaultsManager.setBool(switchButton.isSelected, forKey: .integralAlert)
        }else if md == .open {
            UserDefaultsManager.setBool(switchButton.isSelected, forKey: .gameAlert)
        }else if md == .gift {
            UserDefaultsManager.setBool(switchButton.isSelected, forKey: .giftAlert)
        }else if md == .signin {
            UserDefaultsManager.setBool(switchButton.isSelected, forKey: .signAlert)
        }else if md == .personal {
            UserDefaultsManager.setBool(switchButton.isSelected, forKey: .personalAlert)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class ClubNormalCacheCell: UITableViewCell {
    
    private let bgView = c_make_ex(UIView.self)
        .bgColor(.white).corner(8).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#47494D").font(12.font_medium).text("缓存").end()
    private let valueLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(15.font_medium).end()
    private let cleanButton = c_make_ex(UIButton.self)
        .text("清理").color("#FF7700").font(14.font_medium).end()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            bgView.subviews{
                titleLabel
                valueLabel
                cleanButton
            }
        }
        
        bgView.leading(12.0.Scale).trailing(12.0.Scale).top(0).bottom(16.0.Scale)
        titleLabel.leading(12.0.Scale).top(12.0.Scale)
        valueLabel.leading(12.0.Scale).top(32.0.Scale)
        cleanButton.trailing(10).width(52.0.Scale).height(20.0.Scale).centerVertically()
        
        valueLabel.text = String(format: "%.2fMB", ClubGlobalTool.shared.queryCacheSize())
        
        cleanButton.addTarget(self, action: #selector(clickCleanButton), for: .touchUpInside)
    }
    
    @objc private func clickCleanButton(){
        ClubGlobalTool.shared.clearCache {
            "清理成功".toast_success
            self.valueLabel.text = String(format: "%.2fMB", ClubGlobalTool.shared.queryCacheSize())
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ClubNormalDelCell: UITableViewCell {
    
    private let bgView = c_make_ex(UIView.self)
        .bgColor(.white).corner(8).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#47494D").font(15.font_medium).text("账号注销").end()
    private let rightImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_settings_next").end()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            bgView.subviews{
                titleLabel
                rightImageV
            }
        }
        
        bgView.leading(12.0.Scale).trailing(12.0.Scale).top(0).bottom(16.0.Scale)
        titleLabel.leading(12.0.Scale).centerVertically()
        rightImageV.trailing(12.0.Scale).size(20).centerVertically()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
